package com.example.work3.service;
import com.example.work3.entity.Major;
import com.example.work3.repository.MajorRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MajorService {
    @Autowired
    MajorRepository majorRepository;
    public List<Major> getALl(){
        return (List<Major>) majorRepository.findAll();
    }
    // 增加或更新专业
    public Major addOrUpdateMajor(Major major) {
        return majorRepository.save(major);
    }

    // 删除专业
    public String deleteMajor(Integer id) {
        try {
            majorRepository.deleteById(id);
            return String.valueOf(true);
        } catch (Exception e) {
            return String.valueOf(false);
        }
    }

    // 条件查询专业
    public List<Major> findMajors(String name) {
      if (name != null && !name.isEmpty()) {
            return majorRepository.findByName(name);
        } else {
            return (List<Major>) majorRepository.findAll();
        }
    }
}
